var userId=CommonUtils.uncompile(CommonUtils.getQueryString("userId"));
var form = '';
var orgName = "";
$(document).ready(function(){
	//下拉控件初始化
    initSelect();
    
    //性别
	CommonUtils.findBaseDataRadio("#sex-div","TMOMP_SEX",1,"sex");
	//数据类型
	CommonUtils.findBaseDataRadio("#dataType-div","TMOMP_DATA_TYPE",1,"dataType");
	//单选复选初始化
	$(".i-checks").iCheck({checkboxClass:"icheckbox_square-yellow",radioClass:"iradio_square-yellow"});
	//详情不可编辑
	$("#save-form :input").attr("disabled","disabled");
	//保存
	$("#course-saveBu").click(function(){
		saveFu(1);//添加
	});
	
	//关闭单签选项卡
	$("#course-cancelBu").click(function(){
		closeMenuTab();
	});
	
	
	//填充
	if(userId){
		var dataInfo = findDataInfo(userId);
		if(dataInfo!=null){
			//填充表单数据
			CommonUtils.fillFormData("#save-form",JSON.stringify(dataInfo));
			//填充头像
			fillHeadImg(dataInfo);
			//填充组织
			$("#userOrg").val(dataInfo.orgName);
			$("#orgId").val(dataInfo.orgId);
			orgName = dataInfo.orgName;
			var select = 'dd[lay-value=' + dataInfo.orgId + ']';
			$('#save-form').siblings("div.layui-form-select").find('dl').find(select).click();
		    //用户类型
//		    CommonUtils.findBaseDataOption2("","#userType","TMOMP_USER_TYPE",1,dataInfo.userType);
			//单选复选初始化
			$(".i-checks").iCheck({checkboxClass:"icheckbox_square-yellow",radioClass:"iradio_square-yellow"});
			$("#title-p").html("用户详情");
			//初始化角色选择数据
			initRoleSelect("#roleIds",{orgId:dataInfo.orgId},dataInfo.userRoles);
		}
	}else{
		 //用户类型
//	    CommonUtils.findBaseDataOption("类型","#userType","TMOMP_USER_TYPE",1);
		//初始化角色选择数据
		initRoleSelect("#roleIds",{});
		$("#dataType-div").find("[name=dataType]").on('ifClicked', function(event){
	    	//初始化角色选择数据
			initRoleSelect("#roleIds",{dataType:$(this).val(),orgId:$("#orgId").val()});
		});
		//头像上传点击事件
		$("#head-img").click(function(){
			$("input[name='headImage']").click();
		})
	}
});



//查询用户详细信息
var findDataInfo=function(userId) {
	var dataInfo = null;
	$.ajax({url:'/init/user/findUser',
		data:CommonUtils.encapsulationData({userId:userId}),
		type:'post',  
		dataType:'json',
		async:false,//同步请求
		contentType: "application/json",
		success:function(data) {  
			if(null != data){
				dataInfo = data;
			}
		},
		error : function(req, status, err) { 
			//消息提示款
			errorFunc(req, status, err);
		}  
	});
	return dataInfo;
}

//填充头像
var fillHeadImg = function (dataInfo){
	if(null != dataInfo.headImageUrl && '' != dataInfo.headImageUrl){
		var serverPath = CommonUtils.getServerRoot();
		if(dataInfo.headImageUrl.indexOf("/upload/")>=0 || dataInfo.headImageUrl.indexOf("\\upload\\")>=0){
            $("#head-img").attr("src",serverPath + dataInfo.headImageUrl);
		}else{
            $("#head-img").attr("src",dataInfo.headImageUrl);
		}
		$("#headImageUrl").val(dataInfo.headImageUrl);
	}
}


//查询有效角色集合信息
var findRolesInfo = function(data) {
	var roles = null;
	$.ajax({url:'/init/role/findRoles',
		data:CommonUtils.encapsulationData(data),
		type:'post',  
		dataType:'json',
		async:false,//同步请求
		contentType: "application/json",
		success:function(data) {  
			if(null != data){
				roles = data;
			}
		},
		error : function(req, status, err) { 
			//消息提示款
			errorFunc(req, status, err);
		}  
	});
	return roles;
}

//初始化角色选择数据
var initRoleSelect = function(selectId,data,checkedArray){
	//获取有效角色
	var roles = findRolesInfo(data);
	if(null != roles && roles.length > 0){
		var option = "";
		for(var i = 0; i < roles.length; i++){
			option += '<option value="'+roles[i].roleId+'" hassubinfo="true">'+roles[i].roleName+'</option>';
		}
		$(selectId).html(option).trigger("chosen:updated");
		//默认值
		if(checkedArray && null != checkedArray){
			for(var j = 0; j < checkedArray.length; j++){
				$(selectId + " option[value='"+checkedArray[j].roleId+"']").attr("selected","selected");
			}
			$(selectId).trigger("chosen:updated");
		}
	}else{
		$(selectId).html("").trigger("chosen:updated");
	}
}

